﻿using Assets.Scripts.System;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Scripts.UI
{
    public class SettingPanel : BasePanel
    {
        [Header("子组件")]
        public Slider bgmVolumeSlider;
        public Slider soundVolumeSlider;
        public Image mask;
        public List<Button> keyButtonList;

        private int keyBtnIndex = -1;
        private StorageSystem storageSystem;
        private List<TextMeshProUGUI> keyBtnTextList = new List<TextMeshProUGUI>();
        private Coroutine listenKeyCoroutine = null;

        public override void OnEnter()
        {
            base.OnEnter();
            storageSystem = StorageSystem.Instance;
            bgmVolumeSlider.value = StorageSystem.Instance.storageInfo.BGMVol;
            soundVolumeSlider.value = StorageSystem.Instance.storageInfo.SoundVol;
            foreach (var btn in keyButtonList)
            {
                keyBtnTextList.Add(btn.transform.GetChild(0).GetComponent<TextMeshProUGUI>());
            }
            SetKeyCode(0, storageSystem.storageInfo.attackKeyCode);
            SetKeyCode(1, storageSystem.storageInfo.interactKeyCode);
            SetKeyCode(2, storageSystem.storageInfo.switchWeaponKeyCode);
            SetKeyCode(3, storageSystem.storageInfo.leaveOrPickUpKeyCode);
            SetKeyCode(4, storageSystem.storageInfo.reloadKeyCode);
            SetKeyCode(5, storageSystem.storageInfo.dodgeKeyCode);
            SetKeyCode(6, storageSystem.storageInfo.weaponSkillKeyCode);
            SetKeyCode(7, storageSystem.storageInfo.personSkillKeyCode);
            mask.gameObject.SetActive(false);
        }
        public override void OnExit()
        {
            base.OnExit();
            if (listenKeyCoroutine != null)
            {
                StopCoroutine(listenKeyCoroutine);
            }
        }
        public void OnBackBtnPressed()
        {
            UIManager.Instance.PopPanel();
        }
        public void OnDefaultBtnPressed()
        {
            SoundSystem.Instance.SetBGMVolume(1.0f);
            SoundSystem.Instance.SetSoundVolume(1.0f);
            StorageSystem.Instance.storageInfo.BGMVol = 1.0f;
            StorageSystem.Instance.storageInfo.SoundVol = 1.0f;
            SetKeyCode(0, KeyCode.Mouse0);
            SetKeyCode(1, KeyCode.E);
            SetKeyCode(2, KeyCode.Q);
            SetKeyCode(3, KeyCode.G);
            SetKeyCode(4, KeyCode.R);
            SetKeyCode(5, KeyCode.LeftShift);
            SetKeyCode(6, KeyCode.Mouse1);
            SetKeyCode(7, KeyCode.F);
        }
        public void OnKeyBtnPressed(int keyBtnIndex)
        {
            this.keyBtnIndex = keyBtnIndex;
            mask.gameObject.SetActive(true);
            SetKeyCode(keyBtnIndex, KeyCode.Underscore);
            listenKeyCoroutine = StartCoroutine(Coro_ListenBtn(keyBtnIndex));
        }
        public void SetKeyCode(int keyBtnIndex, KeyCode keyCode)
        {
            keyBtnTextList[keyBtnIndex].text = keyCode == KeyCode.Underscore ? "_" : keyCode.ToString();
            switch (keyBtnIndex)
            {
                case 0:
                    storageSystem.storageInfo.attackKeyCode = keyCode;
                    return;
                case 1:
                    storageSystem.storageInfo.interactKeyCode = keyCode;
                    return;
                case 2:
                    storageSystem.storageInfo.switchWeaponKeyCode = keyCode;
                    return;
                case 3:
                    storageSystem.storageInfo.leaveOrPickUpKeyCode = keyCode;
                    return;
                case 4:
                    storageSystem.storageInfo.reloadKeyCode = keyCode;
                    return;
                case 5:
                    storageSystem.storageInfo.dodgeKeyCode = keyCode;
                    return;
                case 6:
                    storageSystem.storageInfo.weaponSkillKeyCode = keyCode;
                    return;
                case 7:
                    storageSystem.storageInfo.personSkillKeyCode = keyCode;
                    return;
                default:
                    return;
            }
        }
        

        public void BGMVolumeSliderValueChanged()
        {
            SoundSystem.Instance.SetBGMVolume(bgmVolumeSlider.value);
        }
        public void SoundVolumeSliderValueChanged()
        {
            SoundSystem.Instance.SetSoundVolume(soundVolumeSlider.value);
        }

        private IEnumerator Coro_ListenBtn(int keyBtnIndex)
        {
            KeyCode keyCode = KeyCode.None;
            while (!Input.anyKeyDown)
            {
                yield return null;
            }
            foreach (KeyCode key in Enum.GetValues(typeof(KeyCode)))
            {
                if (Input.GetKeyDown(key))
                {
                    keyCode = key;
                    break;
                }
            }
            SetKeyCode(keyBtnIndex, keyCode);
            mask.gameObject.SetActive(false);
            listenKeyCoroutine = null;
            this.keyBtnIndex = -1;
            yield return null;
        }
    }
}